<?php
/**
 * Класс управления событиями в бекенде
 * 
 * @package Phorm_ControlPanel
 * @name /Phorm/ControlPanel/Event.php
 * @copyright phorm.ru
 * @since 11.01.2011
 * @author vladimir@kachalov.net
 */

class Phorm_ControlPanel_Event extends Phorm_Model {
	
	/**
	 * Возвращает список последних событий с материалами, имеющих отношений к текущему пользователю
	 * 
	 * @param array $options Массив опций
	 * 
	 * @return array
	 */
		
	public function getResourcesEvents(array $options = array()) {
		
		$select = $this->_db->select()
				-> from(array("AA"=>"pw_resources_log"),array())
				-> join(array("BB"=>"pw_resources_log"),"AA.resourceid=BB.resourceid",new Zend_Db_Expr("SQL_CALC_FOUND_ROWS BB.*, COUNT(BB.actiontype) amount"))
				-> join(array("CC"=>"pw_users"),"BB.userid=CC.userid",array("editorname"=>"username"))
				-> join(array("DD"=>"pw_resources"),"AA.resourceid=DD.resourceid",array("resourcename","moduleid"))
				-> join(array("EE"=>"pw_resources_modules"),"DD.moduleid=EE.moduleid",array("modulename"))
				-> group(array("BB.resourceid","CC.userid","BB.actiontype"))
				-> limitPage($this->getCurrentPage(),$this->getPerPage());
				
		if(isset($options['only_for_user'])) $select -> where("AA.userid=?",$this->_usersession->UserData["userid"]);
		
		if(isset($options['actiondate_from']) && trim($options['actiondate_from'])!='') {
			$select -> where("BB.actiondate>=".strtotime($options['actiondate_from']));
		}
		
		if(isset($options['actiondate_to']) && trim($options['actiondate_to'])!='') {
			$select -> where("BB.actiondate<=".strtotime($options['actiondate_to']));
		}
		
		if(isset($options['actiontype']) && in_array($options['actiontype'],array('insert','update','delete'))) {
			$select -> where("BB.actiontype=?",$options['actiontype']);
		}
		
		if(isset($options['moduleid'])) {
			$select -> where("DD.moduleid=?",$options['moduleid']);
		}
		
		if(isset($options['sortField']) && in_array($options['sortField'],array('logid','actiondate','resourcename','modulename','actiontype','editorname'))) {

			$sortOrder = isset($options['sortOrder']) && $options['sortOrder']=='desc' ? ' DESC' : ' ASC';
					
			switch ($options['sortField']) {
				case 'logid':
					$select -> order("BB.logid".$sortOrder);
					break;
				case 'actiondate':
					$select -> order("BB.actiondate".$sortOrder);
					break;
				case 'actiontype':
					$select -> order("BB.actiontype".$sortOrder);
					break;
				default:
					$select -> order($options['sortField'].$sortOrder);
			}
		} else {
			$select -> order("BB.logid DESC");
		}
				
		return $this->_db->fetchAll($select);
			
	}
		
		
	/**
	 * Возвращает список последних событий с разделами, имеющих отношений к пользователю
	 * 
	 * @param array $options Массив опций
	 * 
	 * @return array
	 */
		
	public function getCategoriesEvents(array $options = array()) {
		
		$select = $this->_db->select()
				-> from(array("AA"=>"pw_categories_log"), new Zend_Db_Expr("SQL_CALC_FOUND_ROWS COUNT(BB.actiontype) amount"))
				-> join(array("BB"=>"pw_categories_log"),"AA.categoryid=BB.categoryid")
				-> join(array("CC"=>"pw_users"),"BB.userid=CC.userid",array("editorname"=>"username"))
				-> join(array("DD"=>"pw_categories"),"AA.categoryid=DD.categoryid",array("categoryname"))
				-> group(array("BB.categoryid","CC.userid","BB.actiontype"))
				-> limitPage($this->getCurrentPage(),$this->getPerPage());
			
		if(isset($options['only_for_user'])) $select -> where("AA.userid=?",$this->_usersession->UserData["userid"]);
		
		if(isset($options['actiondate_from']) && trim($options['actiondate_from'])!='') {
			$select -> where("BB.actiondate>=".strtotime($options['actiondate_from']));
		}
		
		if(isset($options['actiondate_to']) && trim($options['actiondate_to'])!='') {
			$select -> where("BB.actiondate<=".strtotime($options['actiondate_to']));
		}
		
		if(isset($options['actiontype']) && in_array($options['actiontype'],array('insert','update','delete','shift','move'))) {
			$select -> where("BB.actiontype=?",$options['actiontype']);
		}
		
		if(isset($options['categoryid'])) {
			$select -> where("DD.categoryid=?",$options['categoryid']);
		}
		
		if(isset($options['sortField']) && in_array($options['sortField'],array('logid','actiondate','categoryname','actiontype','editorname'))) {

			$sortOrder = isset($options['sortOrder']) && $options['sortOrder']=='desc' ? ' DESC' : ' ASC';
					
			switch ($options['sortField']) {
				case 'logid':
					$select -> order("BB.logid".$sortOrder);
					break;
				case 'actiondate':
					$select -> order("BB.actiondate".$sortOrder);
					break;
				case 'actiontype':
					$select -> order("BB.actiontype".$sortOrder);
					break;
				default:
					$select -> order($options['sortField'].$sortOrder);
			}
		} else {
			$select -> order("BB.logid DESC");
		}
				
		return $this->_db->fetchAll($select);
			
	}
	
}